﻿using Microsoft.EntityFrameworkCore;
using SmartApplication.Infrastructure.Instrument;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartApplication.Infrastructure.Imface
{
    public class BaseRepository<T>:IBaseRepository<T> where T : class
    {
        public BaseRepository(MyDbContext my)
        {
            My = my;
        }

        public MyDbContext My { get; }

        public async Task<int> AddAsync(T t)
        {
            await My.Set<T>().AddAsync(t);
            return await My.SaveChangesAsync();
        }

        public IQueryable<T> GetAll()
        {
            return  My.Set<T>().AsQueryable();
        }

        public async Task<T> GetValueAsync(long id)
        {
            return await My.Set<T>().FindAsync(id);
        }

        public async Task<int> UpdateAsync(T t)
        {
            My.Set<T>().Update(t);
            return await My.SaveChangesAsync();
        }
    }
}
